package me.pearjelly.model;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 韩孝冰 裁图规则
 */
public class ImgConvertRule {
	int maxWidth;
	int maxHeight;
	Style style;
	String tag;
	String pathRule;

	public ImgConvertRule() {
		this(940, 940, "n");
	}

	public ImgConvertRule(int maxWidth, int maxHeight, String tag) {
		this(maxWidth, maxHeight, Style.AUTO, tag,
				"/pic/#tag#/#key#4#6#/#key#2#4#/Img#key#_#tag#.jpg");
	}

	public ImgConvertRule(int maxWidth, int maxHeight, Style style, String tag,
			String pathRule) {
		this.maxWidth = maxWidth;
		this.maxHeight = maxHeight;
		this.style = style;
		this.tag = tag;
		this.pathRule = pathRule;
	}

	public int getMaxWidth() {
		return maxWidth;
	}

	public void setMaxWidth(int maxWidth) {
		this.maxWidth = maxWidth;
	}

	public int getMaxHeight() {
		return maxHeight;
	}

	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getPathRule() {
		return pathRule;
	}

	public void setPathRule(String pathRule) {
		this.pathRule = pathRule;
	}

	public void setStyle(String value) {
		this.style = Style.valueOf(value);
	}

	public void setStyle(Style style) {
		this.style = style;
	}

	public Style getStyle() {
		return style;
	}

	public String parsePath(String key) {
		String tmpPath = pathRule;
		Matcher matcher = keyPattern.matcher(tmpPath);
		while (matcher.matches() && matcher.groupCount() > 0) {
			String matchStr = matcher.group(1);
			if (matchStr != null) {
				String[] ss = matchStr.split("#");
				int b = Integer.parseInt(ss[2]);
				int e = Integer.parseInt(ss[3]);
				if(b > e){
					int tmp = b;
					b = e;
					e = tmp;
				}
				b = b <= key.length() ? b : 0;
				e = e <= key.length() ? e : key.length();
				tmpPath = tmpPath.replace(matchStr, key.substring(b, e));
				matcher = keyPattern.matcher(tmpPath);
			}
		}
		tmpPath = tmpPath.replace("#key#", key);
		tmpPath = tmpPath.replace("#tag#", this.tag);
		return tmpPath;
	}

	private static final Pattern keyPattern = Pattern
			.compile("^.*(#key#\\d+#\\d+#).*$");

	public enum Style {
		AUTO(1), FIXWIDTH(2), FIXHEIGHT(3);
		int value;

		private Style(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}
	}

	public static void main(String[] args) {
		ImgConvertRule imgConvertRule = new ImgConvertRule();
		long b = System.nanoTime();
		String path = imgConvertRule.parsePath("2521796");
		System.out.println((System.nanoTime()-b));
		System.out.println(path);		
		b = System.nanoTime();
		path = imgConvertRule.parsePath("2521796");
		System.out.println((System.nanoTime()-b));
		System.out.println(path);
		b = System.nanoTime();
		String key = "2521796";
		String tag = "n";
		path = new StringBuilder("/pic/").append(tag)
			.append("/").append(key.substring(5, 7))
			.append("/").append(key.substring(3, 5))
			.append("/Img").append(key).append("_")
			.append(tag).append(".jpg").toString();
		System.out.println((System.nanoTime()-b));
		System.out.println(path);
	}
}
